﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.GuessTheWordsForWP7.Model;
using Studio77.GuessTheWordsForWP7.Core;
using Studio77.GuessTheWordsForWP7.Entity;
using Coding4Fun.Phone.Controls;
using System.Windows.Media.Imaging;

namespace Studio77.GuessTheWordsForWP7
{
    public partial class StagePage : PhoneApplicationPage
    {
        public static StagePage currentStagePage;

        public StagePage()
        {
            InitializeComponent();
            currentStagePage = this;
            this.Loaded += new RoutedEventHandler(StagePage_Loaded);
        }

        StageInfoCollection stages = new StageInfoCollection();

        void StagePage_Loaded(object sender, RoutedEventArgs e)
        {
            //this.Loaded -= new RoutedEventHandler(StagePage_Loaded);

            this.btnReturn.Click += new RoutedEventHandler(btnReturn_Click);
            ProcessUnlock();
            InitStages();
            this.lstStages.DataContext = stages;
            this.gridMask.Visibility = System.Windows.Visibility.Collapsed;
        }

        public void SetLoadEnable()
        {
            this.gridMask.Visibility = System.Windows.Visibility.Visible;
        }

        public void InitStages()
        {
            stages.Clear();
            for (int i = 1; i <= StageManager.StageCount; i++)
            {
                StageInfoViewModel model = new StageInfoViewModel();
                model.Index = i;
                if (StageManager.Instance.stageDatas.ContainsKey(i))
                {
                    StageData sd = StageManager.Instance.stageDatas[i];
                    MapInfo mi = StageManager.Instance.mapInfos[i];
                    model.Score = sd.Score;
                    model.GameTime = (sd.TotalSeconds / 60).ToString() + "'" + (sd.TotalSeconds % 60);
                    model.CompleteCount = sd.ComplateCount.ToString() + "/" + mi.wordsCount.ToString();

                    double rate = getComplateRate(i);
                    if (rate < 0.1f)
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Visible;
                        model.OneStarVisibility = System.Windows.Visibility.Collapsed;
                        model.TwoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.ThreeStarVisibility = System.Windows.Visibility.Collapsed;
                    }
                    else if (rate < 0.4f)
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.OneStarVisibility = System.Windows.Visibility.Visible;
                        model.TwoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.ThreeStarVisibility = System.Windows.Visibility.Collapsed;
                    }
                    else if (rate < 0.8f)
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.OneStarVisibility = System.Windows.Visibility.Collapsed;
                        model.TwoStarVisibility = System.Windows.Visibility.Visible;
                        model.ThreeStarVisibility = System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.OneStarVisibility = System.Windows.Visibility.Collapsed;
                        model.TwoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.ThreeStarVisibility = System.Windows.Visibility.Visible;
                    }

                }
                else
                {
                    model.LockVisibility = System.Windows.Visibility.Visible;
                }
                stages.Add(model);
            }
        }

        /// <summary>
        /// 根据完成情况解锁关卡
        /// </summary>
        private void ProcessUnlock()
        {
            for (int i = 2; i <= StageManager.StageCount; i++)
            {
                if (!StageManager.Instance.stageDatas.ContainsKey(i))
                {
                    // 需要判断解锁
                    if (getComplateRate(i - 1) >= 0.5f)
                    {
                        StageManager.Instance.stageDatas[i] = new StageData();
                        ShowToast("恭喜你解锁了1个新关卡，赠送1个提示灯泡！");
                        StageManager.Instance.LampCount = StageManager.Instance.LampCount + 1;
                    }
                    break;
                }
            }
            StageManager.Instance.saveStageDatas();
        }

        private float getComplateRate(int i)
        {
            if (!StageManager.Instance.stageDatas.ContainsKey(i))
            {
                return 0f;
            }
            return StageManager.Instance.stageDatas[i].ComplateCount
                    / (float)StageManager.Instance.mapInfos[i].wordsCount;
        }

        void btnReturn_Click(object sender, RoutedEventArgs e)
        {
            //fromloading 也就是不需要返回该页
            NavigationService.Navigate(new Uri("/MainPage.xaml?fromloading=1", UriKind.Relative));
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        /// <param name="message"></param>
        public static void ShowToast(String message)
        {
            ShowToast("提示", message, null);
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        public static void ShowToast(String title, String message, BitmapImage icon)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                ToastPrompt toast = new ToastPrompt();
                toast.Title = title;
                toast.Message = message;
                toast.TextOrientation = System.Windows.Controls.Orientation.Horizontal;
                toast.ImageSource = icon;
                toast.Show();
            });
        }
    }
}